// 获取路由

import request from "../utils/request";

/**
 * get function from files
 */
export const getFunctions = () => {
    return request({
        url: '/getFunctions',
        method: 'get'
    })
}

/**
 * update function in files
 */
export function updateFunctions(data) {
    return request({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: '/updateFunctions',
        method: 'post',
        data,
        transformRequest: [function(data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }]
    })
}
/**
 * delete function of files
 */
export function deleteFunctions(data) {
    return request({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: '/deleteFunctions',
        method: 'post',
        data,
        transformRequest: [function(data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }]
    })
}
/**
 * export function by files
 */
export function exportFunctions(data) {
    return request({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: '/exportFunctions',
        method: 'post',
        data,
        transformRequest: [function(data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }]
    })
}
/**
 * run shell command
 * @returns {AxiosPromise}
 */
export function updateGit(data) {
    return request({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: '/updateGit',
        method: 'post',
        data,
        transformRequest: [function(data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }]
    })
}
/**
 * update local package.json
 * @param data
 * @returns {AxiosPromise}
 */
export function updateNpm(data) {
    return request({
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: '/updateNpm',
        method: 'post',
        data,
        transformRequest: [function(data) {
            let ret = ''
            for (const it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }]
    })
}

/**
 * query npm package info
 * @param query
 * @returns {AxiosPromise}
 */
export const getNpmInfo = (query) => {
    return request({
        url: '/getNpmInfo',
        method: 'get',
        params: query
    })
}
export const getGitInfo = (query) => {
    return request({
        url: '/getGitInfo',
        method: 'get',
        params: query
    })
}
export const publishNpm = (query) => {
    return request({
        url: '/publishNpm',
        method: 'get',
        params: query
    })
}
